<?php


namespace app\datacollect\controller;


use app\common\cachemodel\AppConfig;
use Exception;
use think\Controller;
use think\Log;
use think\response\Json;

class ConfigController extends Controller
{

    private static $LOG_PRE = '数据分析平台-采集Sdk配置：';

    /**
     * Android ios 的配置信息
     * @return Json
     * @throws Exception
     */
    public function config(): Json
    {
        $path = $this->request->param('path');//iOS.conf  Android.conf

        $v = input('v', '');//项目 originalVersion
        $nv = input('nv', '');//项目 latestVersion
        $project = input('project', '');//项目 zhunle_local zhunle_beta zhunle 等信息
        $app_id = input('app_id', '');//app id信息 ios：com.liuhe.xzns android:todo 待调研(应该是com.constellation.goddess)

        //异常信息
        $error_msg = '';

        //请求信息
        $request_info = [
            'path' => $path,
            'v' => $v,
            'nv' => $nv,
            'project' => $project,
            'app_id' => $app_id,
            'msg' => ''
        ];

        //env里的配置信息
        $conf_sink = config('conf_sink');

        //服务器配置
        $server_conf = [];
        if ($path === 'iOS.conf')
        {
            //校验ios的项目名和包名是否相同
            if ($project !== $conf_sink['project'])
            {
                $error_msg = 'project异常！';
            }
            if ($app_id !== $conf_sink['app_id_ios'])
            {
                $error_msg = 'app_id异常！';
            }
            if ($error_msg === '')
            {
                $server_conf = (new AppConfig())->configInfo('datacollect_ios_sdk');
            }
        } else if ($path === 'Android.conf')
        {
            //校验android的项目名和包名是否相同
            if ($project !== $conf_sink['project'])
            {
                $error_msg = 'project异常！';
            }
            if ($app_id !== $conf_sink['app_id_android'])
            {
                $error_msg = 'app_id异常！';
            }
            if ($error_msg === '')
            {
                $server_conf = (new AppConfig())->configInfo('datacollect_android_sdk');
            }
        } else
        {//既不是android 也不是ios，报错
            $error_msg = 'path请求异常！';
        }


        //异常信息不为空，那么记录一下
        if ($error_msg)
        {
            //todo 先不抛异常，貌似所有的用户第一次下载App都会请求这个接口【之前认为的是，只有可视化全埋点的时候 才会请求】
//            Log::write(self::$LOG_PRE . '异常信息=' . $error_msg . '，请求信息=' . json_encode($request_info), 'error');
            return json([]);
        } else
        {
            //当前的版本号
            $v_current = $server_conf['v'];
            //把v过滤掉
            unset($server_conf['v']);

            //类型修改
            //disableDebugMode 【待调研】是否不允许debug实时日志调试模式
            if (isset($server_conf['disableDebugMode']))
            {
                $server_conf['disableDebugMode'] = $server_conf['disableDebugMode'] === "true";
            }

            //disableSDK 【待调研】sdk是否不可用
            if (isset($server_conf['disableSDK']))
            {
                $server_conf['disableSDK'] = $server_conf['disableSDK'] === "true";
            }

            //autoTrackMode 【待调研】全埋点模式
            if (isset($server_conf['autoTrackMode']) && $server_conf['autoTrackMode'] !== '')
            {
                $server_conf['autoTrackMode'] = (int)$server_conf['autoTrackMode'];
            }

            //effect_mode 【待调研】远程配置生效模式
            if (isset($server_conf['effect_mode']) && $server_conf['effect_mode'] !== '')
            {
                $server_conf['effect_mode'] = (int)$server_conf['effect_mode'];
            }

            foreach ($server_conf as $k => $v)
            {
                if ($v === '')
                {
                    unset($server_conf[$k]);
                }
            }
            //返回信息
            $data = [
                'v' => $v_current,
                'configs' => $server_conf,//过滤数组为空的值
            ];
            return json($data);
        }
    }
}
